<template>
	<view class="login-container">
		<view class="logo">
			<image src="https://hfd.bjlzkj.com/static/logo.png" mode="aspectFit"></image>
		</view>
		<view class="description">
			使用一键登录，快速便捷
		</view>
		<!-- <button class="wechat-login-btn" @click="wechatLogin">
			一键登录
		</button> -->
		<button type="primary" class="wechat-login-btn" open-type="getPhoneNumber" @getphonenumber="decryptPhoneNumber">获取手机号一键登录</button>
		<view class="footer">
			<text class="footer-text">© 2025 Fitten Tech</text>
		</view>
	</view>
</template>

<script>
	export default {
		data() {
			return {
				loginRes: {},
			};
		},
		onLoad() {
			uni.login({
				provider: 'weixin',
				success: res => {
					console.log(res)
					this.loginRes = res;
				},
				fail: err => {
					uni.showToast({
						title: err.msg,
						icon: 'none'
					})
				}
			});
		},
		methods: {
			// 号码登录，暂时支持微信小程序，后续版本逐渐开发
			async decryptPhoneNumber(e){
				console.log(e)
				var that = this;
				if (e.detail.errMsg != "getPhoneNumber:fail user deny") {
				   await that.$request( '/addons/qingdong/staff/mobilelogin','POST', {
						encryptedData: e.detail.encryptedData,
						iv: e.detail.iv,
						code: that.loginRes.code
					}).then(loginval => {
						if (loginval.code == 1) {
							uni.setStorageSync('user_token', loginval.data.user.token)
							setTimeout(() => {
								that.$request('/addons/qingdong/staff/getInfo', 'GET')
									.then(res => {
										if (res.code == 1) {
											uni.hideLoading()
											uni.setStorageSync('userInfo', res.data)
											uni.showToast({
												title: res.msg,
												icon: 'none'
											})
											setTimeout(() => {
												that.$loginSuccess()
											}, 2000)
										}
									})
							}, 500)
						}
					})
				}
			},
			wechatLogin() {
				uni.showLoading({
					title: '登录中'
				})
				uni.login({
					provider: 'weixin', //使用微信登录
					success: (loginRes) => {
						console.log(loginRes)
						this.$request('/addons/qingdong/staff/wxlogin', 'POST', {
							code: loginRes.code
						}).then(loginval => {
							console.log(loginval)
							if (loginval.code == 1) {
								uni.setStorageSync('user_token', loginval.data.user.token)
								setTimeout(() => {
									this.$request('/addons/qingdong/staff/getInfo', 'GET')
										.then(res => {
											if (res.code == 1) {
												uni.hideLoading()
												uni.setStorageSync('userInfo', res.data)
												uni.showToast({
													title: res.msg,
													icon: 'none'
												})
												setTimeout(() => {
													this.$loginSuccess()
												}, 2000)
											}
										})
								}, 500)
							}
						})
					}
				});
			}
		}
	};
</script>

<style>
	.login-container {
		display: flex;
		flex-direction: column;
		align-items: center;
		justify-content: center;
		height: 100vh;
		background: linear-gradient(45deg, #add8e6, #0076de);
		padding: 40rpx;
		box-sizing: border-box;
	}

	.logo {
		margin-bottom: 400rpx;
	}

	.logo image {
		width: 300rpx;
		height: 300rpx;
	}

	.description {
		font-size: 32rpx;
		margin-bottom: 80rpx;
		color: #666;
		text-align: center;
	}

	.wechat-login-btn {
		height: 100rpx;
		line-height: 100rpx;
		width: 80%;
		background-color: #1aad19;
		color: #fff;
		border: none;
		border-radius: 50rpx;
		font-size: 36rpx;
		display: flex;
		align-items: center;
		justify-content: center;
		box-shadow: 0 8rpx 16rpx rgba(0, 0, 0, 0.1);
	}

	.wechat-icon {
		width: 48rpx;
		height: 48rpx;
		margin-right: 20rpx;
	}

	.footer {
		margin-top: 40rpx;
		text-align: center;
		position: absolute;
		bottom: 60rpx;
	}

	.footer-text {
		font-size: 28rpx;
		color: #999;
	}
</style>